package main

import (
	"fmt"
	"math/rand"
)

func matrix_addition() [8][5]int {
	arr1 := genMatrix()
	arr2 := genMatrix()
	arr3 := [8][5]int{}
	for i := 0; i < 8; i++ {
		for j := 0; j < 5; j++ {
			arr3[i][j] = arr1[i][j] + arr2[i][j]
		}
	}
	return arr3
}

func genMatrix() [8][5]int {
	arr := [8][5]int{}
	for i := 0; i < 8; i++ {
		for j := 0; j < 5; j++ {
			arr[i][j] = rand.Intn(100)
		}
	}
	return arr
}

func season_if(mth uint) {
	if mth > 2 && mth < 6 {
		fmt.Println("Spring")
	} else if mth > 5 && mth < 9 {
		fmt.Println("Summer")
	} else if mth > 8 && mth < 12 {
		fmt.Println("Autumn")
	} else if mth < 3 && mth > 0 || mth == 12 {
		fmt.Println("Winter")
	} else {
		fmt.Println("Wrong number")
	}

}

func season_switch(mth int) {
	switch mth {
	case 3, 4, 5:
		fmt.Println("Spring")
	case 6, 7, 8:
		fmt.Println("Summer")
	case 9, 10, 11:
		fmt.Println("Autumn")
	case 12, 1, 2:
		fmt.Println("Winter")
	default:
		fmt.Println("Wrong number")
	}
}

type student struct {
	name                   string
	chinese, math, english float32
}

type class struct {
	student []student
}

func (s student) s_avg() float32 {
	sum := s.chinese + s.english + s.math
	avg := sum / 3
	return avg
}

func (c class) get_avg_student() {
	for i := range c.student {
		fmt.Println(c.student[i].name, c.student[i].s_avg())
	}
}

func (c class) avg_fail() {
	sum := 0
	for i := range c.student {
		if c.student[i].s_avg() < 60 {
			sum += 1
		}
	}
	fmt.Printf("不及格同学的个数：%d\n", sum)
}

func (c class) avg_subject() {
	var chinese float32 = 0
	var math float32 = 0
	var english float32 = 0
	for i := range c.student {
		chinese += c.student[i].chinese
		math += c.student[i].math
		english += c.student[i].english
	}
	chinese /= float32(len(c.student))
	math /= float32(len(c.student))
	english /= float32(len(c.student))
	fmt.Printf("语文：%.2f, 数学：%.2f, 英语：%.2f\n", chinese, math, english)
}

func main() {
	// 1.随机初始化两个8*5的矩阵，求两个矩阵的和（逐元素相加）
	fmt.Println("====第一题====")
	arr := matrix_addition()
	for i := 0; i < 8; i++ {
		for j := 0; j < 5; j++ {
			fmt.Printf("%5d ", arr[i][j])
		}
		fmt.Println()
	}

	// 2.给定月份，判断属于哪个季节。分别用if和switch实现
	fmt.Println("====第二题====")
	season_if(12)
	season_switch(9)

	// 3.创建一个student结构体，包含姓名和语数外三门课的成绩。用一个slice容纳一个班的同学，求每位同学的平均分和整个班三门课的平均分，全班同学平均分低于60的有几位
	fmt.Println("====第三题====")
	c := class{
		[]student{{"小明", 98.5, 10, 50},
			{"小红", 77, 61.5, 40},
			{"小李", 90, 80, 100},
			{"小张", 60, 60, 60},
			{"小陈", 100, 100, 100},
		},
	}
	// 每位同学的平均分
	c.get_avg_student()
	fmt.Println("==============")
	// 全班同学平均分低于60的有几位
	c.avg_fail()
	fmt.Println("==============")
	// 整个班三门课的平均分
	c.avg_subject()
}
